package http

import (
	"fmt"
	"log"
	http2 "net/http"
	"time"
	"watcher/server/config"
)

type Server struct {
	Server *http2.Server
	Router *Router
}

func (h *Server) Run() {
	h.Router.Register("/write", Writer)
	h.Router.Handle()
	log.Printf("http server listen at %s", h.Server.Addr)
	err := h.Server.ListenAndServe()
	if err != nil {
		log.Fatalf("server start error :%v", err)
	}
}

func NewHttpServer(conf config.Conf) (*Server, error) {
	srv := &http2.Server{
		Addr:         fmt.Sprintf("%s:%d", conf.Http.Host, conf.Http.Port),
		IdleTimeout:  time.Second * 10,
		ReadTimeout:  time.Second * 10,
		WriteTimeout: time.Second * 10,
	}
	return &Server{
		Server: srv,
		Router: NewRouter(srv),
	}, nil
}
